/*
* BlowerDesController.java
* Created on  2015/7/27 12:18
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/27       longshihui    初始版本
*
*/

package com.infitecs.eyas.blower;

import com.infitecs.eyas.blower.model.BlowerDes;
import com.infitecs.eyas.blower.service.BlowerDesService;
import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.AirGasParameterService;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.nature.model.Nature;
import com.infitecs.eyas.nature.service.NatureService;
import com.infitecs.eyas.oncefan.model.OnceFanDes;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/blower/design")
public class BlowerDesController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private BlowerDesService blowerDesService;


    @Autowired
    private EfficiencyService efficiencyService;

    @Autowired
    private AirGasParameterService airGasParameterService;

    @Autowired
    private NatureService natureService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;

    @RequestMapping("")
    public String getPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "blower/design";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "blower/design";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        BlowerDes blowerDes = blowerDesService.getBlowerDes();
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("blowerDes", blowerDes);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "blower/design";
    }

    @RequestMapping(value = "/saveValue", method = RequestMethod.POST)
    @ResponseBody
    public void saveValue(BlowerDes blowerDes) {
        blowerDesService.saveValueOfBlowerDes(blowerDes);
    }

    @RequestMapping(value = "/calculateInit", method = RequestMethod.POST)
    @ResponseBody
    public void calculateInit() {
        sessionService.putBlowerDesInit(sessionService.getCaseId());
    }

    public void getInitBlower() {
        int caseId = sessionService.getCaseId();
        int powerId = sessionService.getPowerId();
        AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(caseId);
        Efficiency efficiency = efficiencyService.getEfficiency(caseId);
        Nature nature = natureService.getNatureByPowerId(powerId);
        BlowerDes blowerDes = blowerDesService.getBlowerDes();
        if (efficiency != null) {
            if (blowerDes.getE1() == null) {
                blowerDes.setE1(efficiency.getE93());
                BlowerDes blowerDes1 = setParamValue("E1", efficiency.getE93());
                blowerDesService.saveValueOfBlowerDes(blowerDes1);
            }
            if (blowerDes.getE5() == null) {
                blowerDes.setE5(efficiency.getE52());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E5", efficiency.getE52()));
            }
        }
        if (airGasParameter != null) {
            if (blowerDes.getE2() == null) {
                blowerDes.setE2(airGasParameter.getHearthAirLeakageRate());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E2", airGasParameter.getHearthAirLeakageRate()));
            }
            if (blowerDes.getE4() == null) {
                blowerDes.setE4(airGasParameter.getAirPreheaterAirLeakageCoefficient());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E4", airGasParameter.getAirPreheaterAirLeakageCoefficient()));
            }
            if (blowerDes.getE3() == null) {
                blowerDes.setE3(airGasParameter.getHearthOverOutAirCoefficient());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E3", airGasParameter.getHearthOverOutAirCoefficient()));
            }
            if (blowerDes.getE6() == null) {
                blowerDes.setE6((double) airGasParameter.getWarehouse());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E6", (double) airGasParameter.getWarehouse()));
            }
            if (blowerDes.getE7() == null) {
                blowerDes.setE7(airGasParameter.getOnceWindRate());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E7", airGasParameter.getOnceWindRate()));
            }
            if (nature != null) {
                if (blowerDes.getE15() == null) {
                    blowerDes.setE15(nature.getAnnualAverageTemperature());
                    blowerDesService.saveValueOfBlowerDes(setParamValue("E15", nature.getAnnualAverageTemperature()));
                }
                if (blowerDes.getE16() == null) {
                    blowerDes.setE16(nature.getSummerOutdoorCalculateTemperature());
                    blowerDesService.saveValueOfBlowerDes(setParamValue("E16", nature.getSummerOutdoorCalculateTemperature()));
                }
            }
        }
    }

    private BlowerDes setParamValue(String id, Double value) {
        BlowerDes blowerDes = new BlowerDes();
        blowerDes.setParamValue(value);
        blowerDes.setParamName(id);
        return blowerDes;
    }

    @RequestMapping(value = "/getRecover", method = RequestMethod.GET)
    @ResponseBody
    public BlowerDes getRecover() {
        getInitBlower();
        return blowerDesService.getBlowerDes();
    }


    @RequestMapping(value = "/Re-reference", method = RequestMethod.POST)
    @ResponseBody
    public BlowerDes reReference() {
        int caseId = sessionService.getCaseId();
        int powerId = sessionService.getPowerId();
        AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(caseId);
        Efficiency efficiency = efficiencyService.getEfficiency(caseId);
        Nature nature = natureService.getNatureByPowerId(powerId);
        BlowerDes blowerDes = blowerDesService.getBlowerDes();
        if (efficiency != null) {

            blowerDes.setE1(efficiency.getE93());
            BlowerDes blowerDes1 = setParamValue("E1", efficiency.getE93());
            blowerDesService.saveValueOfBlowerDes(blowerDes1);


            blowerDes.setE5(efficiency.getE52());
            blowerDesService.saveValueOfBlowerDes(setParamValue("E5", efficiency.getE52()));

        }
        if (airGasParameter != null) {

            blowerDes.setE2(airGasParameter.getHearthAirLeakageRate());
            blowerDesService.saveValueOfBlowerDes(setParamValue("E2", airGasParameter.getHearthAirLeakageRate()));


            blowerDes.setE4(airGasParameter.getAirPreheaterAirLeakageCoefficient());
            blowerDesService.saveValueOfBlowerDes(setParamValue("E4", airGasParameter.getAirPreheaterAirLeakageCoefficient()));


            blowerDes.setE3(airGasParameter.getHearthOverOutAirCoefficient());
            blowerDesService.saveValueOfBlowerDes(setParamValue("E3", airGasParameter.getHearthOverOutAirCoefficient()));


            blowerDes.setE6((double) airGasParameter.getWarehouse());
            blowerDesService.saveValueOfBlowerDes(setParamValue("E6", (double) airGasParameter.getWarehouse()));


            blowerDes.setE7(airGasParameter.getOnceWindRate());
            blowerDesService.saveValueOfBlowerDes(setParamValue("E7", airGasParameter.getOnceWindRate()));

            if (nature != null) {

                blowerDes.setE15(nature.getAnnualAverageTemperature());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E15", nature.getAnnualAverageTemperature()));

                blowerDes.setE16(nature.getSummerOutdoorCalculateTemperature());
                blowerDesService.saveValueOfBlowerDes(setParamValue("E16", nature.getSummerOutdoorCalculateTemperature()));

            }
        }
        return blowerDesService.getBlowerDes();
    }

    @RequestMapping(value = "blowerDesDesSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse blowerDesDesSave(BlowerDes blowerDes) {
        try {
            blowerDesService.blowerDesDesSave(blowerDes);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
